<?php
namespace app\manage\controller;

use app\manage\model\Role as m_Role;
use app\manage\model\RoleAction;
use think\Controller;
use think\migration\command\migrate\Status;
use think\Request;
use think\Validate;
use think\Db;
use Config;
use app\common\model\TokenCheck;
use app\manage\model\OperationLog;
use app\manage\model\AdminUser;

class PlatformAccount extends Controller{
    public function __construct(Request $request = null)
    {
//        parent::__construct($request);
    }

    /**
     * 显示平台账户
     * @param   keyword         关键字
     * @param   p               页码
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
//    public function showPlatformAccount() {
//        $input = input();
//        $keyword = isset($input['keyword']) ? trim($input['keyword']) : "";
//        $page = isset($input['p']) ? trim($input['p']) : "";
//        $create_id = isset($input['admin_id']) ? trim($input['admin_id']) : "";
//
//        /**
//         * 获取一页数据和总数，尚未进行错误判断与处理
//         */
//        $model=new AdminUser();
//        $onePage = $model->onePage($page, get_list_rows(), $keyword, $create_id,1);
//        $total = $model->total($keyword, $create_id,1);
//
//        $data['total'] = $total;
//        $data['data'] = $onePage;
//        returnJson(200,"成功", $data)->send();die;
//    }

    /**
     * 展示平台账户，仅针对超管
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function showPlatformAccount() {
        $input = input();
        $type = isset($input['type']) ? trim($input['type']) : "";// 空字符串表示所有，1表示智慧公寓，2表示智慧厂区，3表示智慧校园，4表示智慧农业
        $page = isset($input['p']) ? trim($input['p']) : "";

        $model=new AdminUser();
        $onePage = $model->onePage($page, get_list_rows(), '', 1, 1, '', $type);
        $total = $model->total('', 1, 1, '', $type);

        $data['total'] = $total;
        $data['data'] = $onePage;
        returnJson(200,"成功", $data)->send();die;
    }

    /**
     * 增加平台账户
     * @param   name    平台名称
     * @param   type    平台类型
     * @param   address    地址  XX省XX市
     * @param   street    详细地址  XX区XX街XX号
     * @param   admin    管理员
     * @param   admin_tel    手机号
     * @param   admin_pwd    密码
     * @param   add_permission    附加权限  营收统计,数据云存储，若没有，传空字符串
     */
    public function createPlatformAccount() {
        $input = input();
        $validate = new Validate([
            ['name', 'require|max:50', '名称不能为空|不能超过20字'],
            ['type', 'require', '类型不能为空'],
            ['address', 'require', '地址不能为空'],
            ['street', 'require', '地址不能为空'],
            ['admin', 'require|max:50', '管理员不能为空|不能超过20字'],
            ['admin_tel', 'require|max:11|/^1[3-8]{1}[0-9]{9}$/', '手机号码不能为空|手机号码不能超过11个字符|手机号码格式不正确'],
            ['admin_pwd', 'require|min:6|max:256', '密码不能为空|密码长度不能小于6位|密码长度不能大于12位']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $data['key'] = rand(100000, 999999);// 密码加密码
        $data['name'] = isset($input['name']) ? trim($input['name']) : "";
        $data['type'] = isset($input['type']) ? trim($input['type']) : "";
        $data['address'] = isset($input['address']) ? trim($input['address']) : "";
        $data['street'] = isset($input['street']) ? trim($input['street']) : "";
        $data['admin'] = isset($input['admin']) ? trim($input['admin']) : "";
        $data['admin_tel'] = isset($input['admin_tel']) ? trim($input['admin_tel']) : "";
        $data['admin_pwd'] = isset($input['admin_pwd']) ? md5(trim($input['admin_pwd']).$data['key']) : "";
        $data['create_id'] = isset($input['admin_id']) ? trim($input['admin_id']) : "";
//        $data['add_permission'] = isset($input['add_permission']) ? trim($input['add_permission']) : "";
        $data['add_permission'] = '房源管理,租客管理,财务管理,公寓管理';
        $data['admin_status'] = 1;
        $data['admin_sign'] = 1;
        $data['create_time'] = time();
        $data['update_time'] = time();

        /**
         * 平台账户表插入账户
         */
        $model=new AdminUser();

        // 验证唯一性
        if (($model->uniqueness($data['admin_tel'])) !== 0) {
            returnJson(-2012,"账号已存在")->send();die;
        }

        $addtextRes=$model->m_insert($data);
        if (!$addtextRes) {
            returnJson(-2001,"服务器错误")->send();die;
        }

        /**
         * 每个平台账户创建后都增加经纪人角色
         */
        $data_a['name'] = '经纪人';
        $data_a['is_platform'] = '2';
        $data_a['is_department'] = '2';
        $data_a['create_id'] = $addtextRes;
        $data_a['number'] = 0;
        $data_a['status'] = 1;
        $data_a['create_time'] = time();
        $data_a['update_time'] = time();

        $model=new m_Role();
        $addtextRes = $model->m_insert($data_a);
        $model = new RoleAction();
        $actions = $model->setActionRelation($addtextRes, '100,101,102,103,104,110,111,112,113,114,119,120', '10,100,101,102,103,104,13,110,14,111,15,112,113,114,119,120,18');//初始化经纪人角色权限
        if ($actions) {
            returnJson(200,"成功", $addtextRes)->send();die;
        } else {
            returnJson(-2001,"服务器错误")->send();die;
        }
    }

    /**
     * 更新平台账户
     * @param   admin_id
     * @param   name    平台名称
     * @param   type    平台类型
     * @param   address    地址  XX省XX市
     * @param   street    详细地址  XX区XX街XX号
     * @param   admin    管理员
     * @param   admin_tel    手机号
     * @param   admin_status  状态 1为运行中 2为已停运
     * @param   add_permission    附加权限  营收统计,数据云存储，若没有，传空字符串
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editPlatformAccount() {
        $input = input();
        $validate = new Validate([
            ['admin_id', 'require', '平台id不能为空'],
            ['name', 'require|max:50', '名称不能为空|不能超过20字'],
            ['type', 'require', '类型不能为空'],
            ['address', 'require', '地址不能为空'],
            ['street', 'require', '地址不能为空'],
            ['admin', 'require|max:50', '管理员不能为空|不能超过20字'],
            ['admin_tel', 'require|max:11|/^1[3-8]{1}[0-9]{9}$/', '手机号码不能为空|手机号码不能超过11个字符|手机号码格式不正确'],
            ['admin_status', 'require', '状态不能为空']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $admin_id = isset($input['admin_id']) ? trim($input['admin_id']) : "";
        $data['name'] = isset($input['name']) ? trim($input['name']) : "";
        $data['type'] = isset($input['type']) ? trim($input['type']) : "";
        $data['address'] = isset($input['address']) ? trim($input['address']) : "";
        $data['street'] = isset($input['street']) ? trim($input['street']) : "";
        $data['admin'] = isset($input['admin']) ? trim($input['admin']) : "";
        $data['admin_tel'] = isset($input['admin_tel']) ? trim($input['admin_tel']) : "";
        $data['add_permission'] = isset($input['add_permission']) ? trim($input['add_permission']) : "";
        $data['admin_status'] = isset($input['admin_status']) ? trim($input['admin_status']) : "";
        $data['update_time'] = time();

        $model=new AdminUser();
        $edittextRes=$model->m_update($admin_id, $data);
        if ($edittextRes) {
            returnJson(200,"成功")->send();die;
        } else {
            returnJson(-2013,"数据无变化")->send();die;
        }
    }

    /**
     * @param   admin_id
     * @param   admin_pwd
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function updatePassword() {
        $input = input();
        $validate = new Validate([
            ['admin_id', 'require', '平台账户id不能为空'],
            ['admin_pwd', 'require|min:6|max:256', '密码不能为空|密码长度不能小于6位|密码长度不能大于12位']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $admin_id = isset($input['admin_id']) ? trim($input['admin_id']) : "";
        $data['admin_pwd'] = isset($input['admin_pwd']) ? trim($input['admin_pwd']) : "";

        // 获取密码加密时的key
        $model=new AdminUser();
        $key_info = $model->getKey($admin_id);
        $key = $key_info['key'];

        $data['admin_pwd'] = md5($data['admin_pwd'] . $key);
        $edittextRes = $model->m_update($admin_id, $data);
        if ($edittextRes) {
            returnJson(200,"成功")->send();die;
        } else {
            returnJson(-2013,"数据无变化")->send();die;
        }
    }

    /**
     * @param   admin_id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function deletePlatformAccount() {
        $input = input();
        $validate = new Validate([
            ['admin_id', 'require', '平台id不能为空']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $admin_id = isset($input['admin_id']) ? trim($input['admin_id']) : "";

        $model=new AdminUser();
        $data['admin_status'] = 3;
        $deletetextRes=$model->m_update($admin_id, $data);
        if ($deletetextRes) {
            returnJson(200,"成功")->send();die;
        } else {
            returnJson(-2013,"数据无变化")->send();die;
        }
    }

    /**
     * @param   admin_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detailPlatformAccount() {
        $input = input();
        $validate = new Validate([
            ['admin_id', 'require', '平台id不能为空']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $admin_id = isset($input['admin_id']) ? trim($input['admin_id']) : "";

        $model=new AdminUser();
        $detailtextRes=$model->detail($admin_id, 1);
        if ($detailtextRes) {
            returnJson(200,"成功", $detailtextRes)->send();die;
        } else {
            returnJson(-2001,"服务器错误")->send();die;
        }
    }

}
